package com.lingyun.ucm.web.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

import com.lingyun.ucm.common.pojo.bean.ClientReport;
import com.lingyun.ucm.common.pojo.bean.ConfigAcquireFailLog;
import com.lingyun.ucm.common.rmi.IRMIServerService;
import com.lingyun.ucm.web.mvc.dao.ClientReportDao;
import com.lingyun.ucm.web.mvc.dao.ConfigAcquireFailLogDao;

/**
 * 
 * @author Terrence2013
 * 2018年1月24日 上午9:57:36
 * TODO
 */
public class RMIServerServiceImpl extends UnicastRemoteObject implements IRMIServerService {
    /**
     *
     */
    private static final long serialVersionUID = -271947229644133464L;

    private ConfigAcquireFailLogDao configAcquireFailLogDao = new ConfigAcquireFailLogDao();
    private ClientReportDao clientReportDao = new ClientReportDao();
    
    public RMIServerServiceImpl() throws RemoteException {}
    /**
     * 将客户端相关信息记录到内存数据库中
     * @param clientIp
     */
    @Override
    public void receiveHearbeatFromClient(String clientIp) throws RemoteException {
        ClientReport clientReport = clientReportDao.get(clientIp);
        Date now = new Date();
        if (clientReport == null) {
            clientReport = new ClientReport();
            clientReport.setIp(clientIp);
            clientReport.setFirst_time(now);
            clientReport.setLast_time(now);
            clientReportDao.insert(clientReport);
        } else {
            clientReport.setLast_time(now);
            clientReportDao.update(clientReport);
        }
    }
    
    /**
     * 记录读取配置失败日志
     * @param clientIp
     * @param configKey
     * @param failCause
     * @throws RemoteException
     */
    @Override
    public void log(String clientIp, String configKey, String failCause) throws RemoteException {
        ConfigAcquireFailLog configAcquireFailLog = new ConfigAcquireFailLog();
        configAcquireFailLog.setIp(clientIp);
        configAcquireFailLog.setFail_message(configAcquireFailLog.appendMessage(configKey, failCause));
        configAcquireFailLog.setFail_time(new Date());
        configAcquireFailLogDao.insert(configAcquireFailLog);
    }
}
